#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 100000;
struct node
{
    int index, data;
} nod[MAXN];
bool cmp(node ll, node rr)
{
    return ll.data <= rr.data;
}
int main()
{
    int N, first;
    scanf("%d%d", &N, &first);
    int useless; //这是没有考虑无效结点的情况
    int i;
    for (i = 0; i < N; ++i)
    {
        scanf("%d%d", &nod[i].index, &nod[i].data);
        scanf("%d", &useless);
    }
    sort(nod, nod + N, cmp);
    printf("%d %05d\n", N, nod[0].index);
    for (i = 0; i < N - 1; ++i)
    {
        printf("%05d %d %05d\n", nod[i].index, nod[i].data, nod[i + 1].index);
    }
    printf("%05d %d -1\n", nod[i].index, nod[i].data);
    return 0;
}